package com.zhuiyun.pm.client.fb.controller;
import com.alibaba.fastjson.JSONArray;
import com.zhuiyun.pm.client.fb.entity.ClientGoodInfo;
import com.zhuiyun.pm.client.fb.entity.Specs;
import com.zhuiyun.pm.client.fb.service.PublishService;
import com.zhuiyun.pm.config.CommonResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
 * @ClassName publishSaveAdd
 * Description 发布功能
 * Author Administrator
 * @Date 2023/3/13 publishSaveAdd
 * Version 1.0
 **/
@RestController
@RequestMapping("publish")
@CrossOrigin
public class PublishController {

    @Autowired
    PublishService publishService;

    /**
     * 拍品发布功能
     * @author 温
     * @date 2023/3/13 14:45
     * @parameter [file, publish]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @PostMapping("/publishSaveAdd")
    public CommonResult publishSaveAdd(MultipartFile[] goodsImg, MultipartFile[] viewImg, ClientGoodInfo publish, String JSONspecs) {
        List<Specs> specs = JSONArray.parseArray(JSONspecs, Specs.class);
        int i = publishService.publishAdd(goodsImg,viewImg, publish,specs);
        if(i>0){
            return CommonResult.ok();
        }else{
            return CommonResult.error("添加失败");
        }

       }

    /**
     * 设置拍卖信息
     * @author 温
     * @date 2023/3/13 15:45
     * @parameter [publish]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @PostMapping("/publishUpdate")
    public CommonResult publishUpdate(@RequestBody ClientGoodInfo publish){
        int i = publishService.publishUpdate(publish);
        if(i>0){
            return CommonResult.ok();
        }else{
            return  CommonResult.error("修改失败");
        }
    }

    /**
     * 根据id查询竞拍详情和商品详情
     * @author 温
     * @date 2023/3/14 9:04
     * @parameter [id]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @GetMapping("/publishId/{id}")
    public CommonResult publishId(@PathVariable("id")int id){
        ClientGoodInfo publishid = publishService.publishid(id);
        return CommonResult.ok(publishid);
    }

    /**
     * @description:查询商品属性
     * @author: dongdong
     * @date: 2023/3/17 8:23
     * @param: [goodsNumber]
     * @return: com.zhuiyun.pm.config.CommonResult
    **/
    @GetMapping("/getSpecsByGoodId")
    public CommonResult getSpecsByGoodId( String goodsNumber){
        System.out.println("goodsNumber"+goodsNumber);
        List<Specs> specs = publishService.getSpecsByGoodId(goodsNumber);
        return CommonResult.ok(specs);
    }

    /**
     * 修改发布的信息
     * @author 温
     * @date 2023/3/20 10:22
     * @parameter [file, publish]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @PostMapping("/publishUpdates")
    public CommonResult publishUpdates(MultipartFile[] file,ClientGoodInfo publish){
        int i = publishService.publishUpdates(file, publish);
        if(i>0){
            return  CommonResult.ok();
        }else{
            return CommonResult.error("修改失败");
        }
    }
    /**
    * @Author 赵笛
    * @Description TODO 修改多文件发布商品信息
    * @Date 2023/3/30 14:43
    * @Return com.zhuiyun.pm.config.CommonResult
    * @param goodsImg
    * @param viewImg
    * @param publish
    */
    @PostMapping("/updatePublish")
    public CommonResult updatePublish(MultipartFile[] goodsImg, MultipartFile[] viewImg,ClientGoodInfo publish){
        int i = publishService.updatePublish(goodsImg,viewImg,publish);
        if(i>0){
            return  CommonResult.ok();
        }else{
            return CommonResult.error("修改失败");
        }
    }
    /**
     * 单图片修改
     * @author 温
     * @date 2023/3/20 10:22
     * @parameter [file, publish]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @PostMapping("/publishUpdatesAnd")
    public CommonResult publishUpdatesAnd(MultipartFile file,ClientGoodInfo publish){
        int i = publishService.publishUpdateAns(file, publish);
        if(i>0){
            return  CommonResult.ok();
        }else{
            return CommonResult.error("修改失败");
        }
    }



/**
 * 首页模糊查
 * @author 温
 * @date 2023/4/3 19:25
 * @parameter [goodName, clientGoodInfo]
 * @return com.zhuiyun.pm.config.CommonResult
 */
    @GetMapping("/publishMo")
    public CommonResult publishMo(String goodsName,String startPrice){
        if (StringUtils.isBlank(goodsName)) {
            return CommonResult.error("商品名称不能为空");
        }
        List<ClientGoodInfo> clientGoodInfos = publishService.publishMO(goodsName,startPrice);
        return CommonResult.ok(clientGoodInfos);
    }

    /**
     * 模糊查查询出商品信息 根据价格从高到底来进行排序
     * @author 温
     * @date 2023/4/6 13:44
     * @parameter [goodName, startPrice]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @GetMapping("/clientPrice")
    public CommonResult clientPrice(String goodName,String startPrice){
        List<ClientGoodInfo> clientGoodInfos = publishService.clientPrice(goodName,startPrice);
        return CommonResult.ok(clientGoodInfos);
    }
    /**
     * 模糊查查询出商品信息 根据价格从低到高来进行排序
     * @author 温
     * @date 2023/4/6 13:44
     * @parameter [goodName, startPrice]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @GetMapping("/cientPrices")
    public CommonResult clientPrices(String goodName,String startPrice){
        List<ClientGoodInfo> clientGoodInfos = publishService.clientPrices(goodName,startPrice);
        return CommonResult.ok(clientGoodInfos);
    }

}
